<?php

/**
 * Description of RepoGabinete
 *
 * @author  Mariano Renkine
 * @author  Fernando Arau
 */
class RepoGabinete extends Repositorio {

    function __construct() {
        $this->tabla = "gabinete";
        $this->id = "idGabinete";
    }

    function agregar(Gabinete $gabinete) {
        try {

            $link = $this->OpenDB();
            $this->beginTransaction();
            $query = "INSERT INTO " . $this->tabla . "(nombre, descripcion, dimension, usuario, deshabilitado) values (";
            $query .= "'" . $gabinete->getNombre() . "', ";
            $query .= "'" . $gabinete->getDescripcion() . "', ";
            $query .= $gabinete->getDimension() . ", ";
            $query .= "'" . $gabinete->getUsuario() . "', ";
            $query .= $gabinete->getDeshabilitado() ? 1 : 0 . ")";
            $result = $this->PostSQL($query);
            if ($result) {
                $this->commitTransaction();
            } else {
                $this->rollbackTransaction();
            }
            $this->CloseDB($link);
            return $result;
        } catch (Exception $ex) {
            return $ex;
        }
    }

    function modificar(Gabinete $gabinete) {
        try {
            $link = $this->OpenDB();
            $this->beginTransaction();
            if ($this->VerificarVersion($gabinete->getVersion(), $gabinete->getId())) {
                $query = "UPDATE " . $this->tabla . " SET ";
                $query .= "descripcion = '" . $gabinete->getDescripcion() . "', ";
                $query .= "usuario = '" . $gabinete->getUsuario() . "', ";
                $query .= "deshabilitado = " . $gabinete->getDeshabilitado() . ", ";
                $query .= "dimension = " . $gabinete->getDimension() . ", ";
                $query .= "nombre = '" . $gabinete->getNombre() . "' ";
                $query .= " WHERE " . $this->id . " = " . $gabinete->getId();
                $result = $this->PostSQL($query);
                $this->commitTransaction();
                $this->CloseDB($link);
                return $result;
            } else {
                $this->rollbackTransaction();
                $this->CloseDB($link);
                throw new Exception("El registro ha sido modificado por otro usuario", ERROR_VERSION);
            }
        } catch (Exception $ex) {
            return $ex->getCode() + "|" + $ex->getMessage();
        }
    }

    function buscar($id) {
        $link = $this->OpenDB();
        $queryTxt = "SELECT  nombre, descripcion, dimension, deshabilitado, CONVERT(BIGINT,version) AS version
      FROM " . $this->tabla . " WHERE " . $this->id . " = " . $id;
        $query = $this->GetSQL($queryTxt);
        $gabinete = new Gabinete();
        $gabinete->setDescripcion($query[0]->descripcion);
        $gabinete->setVersion($query[0]->version);
        $gabinete->setDeshabilitado($query[0]->deshabilitado);
        $gabinete->setDimension($query[0]->dimension);
        $gabinete->setNombre($query[0]->nombre);
        $gabinete->setId($id);
        $this->CloseDB($link);
        return $gabinete;
    }

    function validarDisponibilidad($fecha, $hora, $gabinete) {
        $link = $this->OpenDB();
        $subQuery = "SELECT gabinete FROM gabinete_bloqueo WHERE desde >= '" . $this->SqlDateTime($fecha, $hora) . "' AND (hasta IS NULL or hasta <='" . $this->SqlDateTime($fecha, $hora) . "')";
        $where = "fechaReserva = '" . $this->SqlDate($fecha) . " $hora:00:00' 
            AND gabinete = '" . $gabinete . "' AND gabinete NOT IN (" . $subQuery . ")";
        $query = $this->GetTableDataByWhere('reserva', $where);
        $this->CloseDB($link);
        return ($query) ? 0 : 1;
    }

    function validarDisponibilidadSinReservaActual($fecha, $hora, $gabinete, $reserva) {
        $link = $this->OpenDB();
        $where = "fechaReserva = '" . $this->SqlDate($fecha) . " $hora:00:00' AND gabinete = '" . $gabinete . "' AND idReserva <> " . $reserva;
        $query = $this->GetTableDataByWhere('reserva', $where);
        if ($query != false) {
            return 0;
        } else {
            return 1;
        }
        $this->CloseDB($link);
    }

    function listarHabilitados() {
        $link = $this->OpenDB();
        $query = "SELECT idGabinete, nombre, descripcion, dimension, CONVERT(BIGINT,version) AS version FROM " . $this->tabla . " WHERE deshabilitado = 0";
        $gabinetesObj = $this->GetSQL($query);
        $gabinetes = null;
        foreach ($gabinetesObj as $gabineteObj) {
            $gabinete = new Gabinete();
            $gabinete->setDescripcion($gabineteObj->descripcion);
            $gabinete->setDimension($gabineteObj->dimension);
            $gabinete->setNombre($gabineteObj->nombre);
            $gabinete->setId($gabineteObj->idGabinete);
            $gabinete->setVersion($gabineteObj->version);
            $gabinete->setDeshabilitado(0);
            $gabinetes[] = $gabinete;
        }
        $this->CloseDB($link);
        return $gabinetes;
    }

    function listarNoBloqueados($fecha, $hora) {
        $link = $this->OpenDB();
        $subQuery = "SELECT gabinete FROM gabinete_bloqueo WHERE desde >= '" . $this->SqlDateTime($fecha, $hora) . ":00:00.000' AND (hasta IS NULL or '" . $this->SqlDateTime($fecha, $hora) . ":00:00.000' <= hasta)";
        $query = "SELECT idGabinete, nombre, descripcion, dimension, CONVERT(BIGINT,version) AS version 
      FROM " . $this->tabla . " WHERE deshabilitado = 0 AND idGabinete not in (" . $subQuery . ")";
        $gabinetesObj = $this->GetSQL($query);
        $gabinetes = null;
        foreach ($gabinetesObj as $gabineteObj) {
            $gabinete = new Gabinete();
            $gabinete->setDescripcion($gabineteObj->descripcion);
            $gabinete->setDimension($gabineteObj->dimension);
            $gabinete->setNombre($gabineteObj->nombre);
            $gabinete->setId($gabineteObj->idGabinete);
            $gabinete->setVersion($gabineteObj->version);
            $gabinete->setDeshabilitado(0);
            $gabinetes[] = $gabinete;
        }
        $this->CloseDB($link);
        return $gabinetes;
    }

    function estaBloqueado($fecha, $hora, $gabinete) {
        $link = $this->OpenDB();
        $query = "SELECT gabinete FROM gabinete_bloqueo WHERE desde >= '" . $this->SqlDateTime($fecha, $hora) . ":00:00.000' AND (hasta IS NULL or '" . $this->SqlDateTime($fecha, $hora) . ":00:00.000' <= hasta) AND gabinete = " . $gabinete;
        $gabineteObj = $this->GetSQL($query);
        $this->CloseDB($link);
        return ($gabineteObj) ? 1 : 0;
    }

}

?>
